using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	public Texture hud;
	public Texture hud1;
	public Texture hud2;
	public Texture hud3;
	public Transform Besourinho, BadSpider, Abelhuda;
	Texture current_hud;
	
	public enum GameState{MAINMENU, PLAYING, PAUSE, WON, LOST, };
	
	public GameState gameState;
	
	public static GameManager thisInstance;
	
	private UIManager mUI;
    private UIManager MUI {
        get {
            if(mUI == null) {
                 mUI = (UIManager)FindObjectOfType(typeof(UIManager));
            }
            return mUI;
        }
    }
	
	int totalEnemys;
	
	int blastEnemys;
	
	//Variáveis - Cuidado ao substituí-las no futuro por seus respectivos contadores
	int vidas = 0;
	//int special = 0;
	
	void Awake(){
	
		Resolution[] resolutions = Screen.resolutions;
        foreach (Resolution res in resolutions) {
            print(res.width + "x" + res.height);
			Debug.Log(res.width + "x" + res.height);
        }
        Screen.SetResolution(resolutions[0].width, resolutions[0].height, true);
		Debug.Log("Utilizado" + resolutions[0].width + "x" + resolutions[0].height);
		
		//Tratamento para caso já haja uma instancia do jogo
		if (thisInstance != null && thisInstance != this) {
            Destroy(gameObject);
			Debug.Log("vidas" + vidas);
		}
        else
        {
            thisInstance = this;
            //DontDestroyOnLoad(this);
        }
		
		vidas = 3;
		
		//special = 0;
		
		blastEnemys = 0;
		
		gameState = GameState.PLAYING;
		
		CreateEnemies(BadSpider, Besourinho, Abelhuda, 15);
		
		totalEnemys = GameObject.FindGameObjectsWithTag("Enemy").Length; 
				
		Time.timeScale = 1.0f;
		
		current_hud = hud3;
		
		//não importa o caminho da pasta, apenas o nome da cena
		//Application.LoadLevel("IntroScene");
		
	}
	
	void OnGUI(){
	
		switch(gameState){
		
		case GameState.PLAYING:
			GUILayout.BeginArea(new Rect((Screen.width*0.1f), (Screen.height-(Screen.height*0.25f)), (Screen.width*0.1f), (Screen.height*0.3f)), current_hud);
				GUILayout.BeginVertical();
	        	GUILayout.Space (20);
					//horiz
					GUILayout.BeginHorizontal();
					GUILayout.FlexibleSpace();
					GUILayout.Label("Score: ");
					GUILayout.FlexibleSpace();
					GUILayout.EndHorizontal();
					//horiz
					GUILayout.BeginHorizontal();
					GUILayout.FlexibleSpace();
					GUILayout.Label(blastEnemys + "/" + totalEnemys, GUILayout.Height(20));
					GUILayout.FlexibleSpace();
					GUILayout.EndHorizontal();
				GUILayout.FlexibleSpace();
				GUILayout.EndVertical();
			GUILayout.EndArea();
			break;
			
		case GameState.WON:
			
			GUILayout.Label("Voce Ganhou!");
			
			if(GUILayout.Button("Jogue Novamente!"))
			{
				
				Application.LoadLevel(Application.loadedLevel);
			}
			
			break;
			
		case GameState.PAUSE:
			GUILayout.Label("PAUSE - Pressione ESC para voltar");
			
			break;
			
		}
		
	}
	
	void Update()
    {
        if (gameState == GameState.MAINMENU)
        {
            GameMainMenu();
        }

        /// Pausing
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            GamePause();
        }
    }
	
	public void DestroyEnemy(){
		
		blastEnemys++;
		
		if(blastEnemys >= totalEnemys){
		
			WonGame();
		}
		
	}
	
	
	
	void WonGame(){
	
		gameState = GameState.WON;
		//useGUILayout = false;
		Time.timeScale = 0.0f;
		Application.LoadLevel("Won");
	}
	
	public void LostLife() {
		vidas--;
		
		switch(vidas)
		{
		case 0:
			current_hud = hud;
			LostGame();
			break;			
		case 1:
			current_hud = hud1;
			break;
		case 2:
			current_hud = hud2;
			break;
			
		case 3:
			current_hud = hud3;
			break;			
		}
		
	}
	
	public void LostGame(){
	
		gameState = GameState.LOST;
		//useGUILayout = false;
		
		Time.timeScale = 0.0f;
		Debug.Log("Saindo do jogo");
		thisInstance = null;
		
		Application.LoadLevel("GameOver");
		
	}
	
	public void GamePause()
    {
        if (gameState == GameState.PLAYING)
        {
            //mUI.GamePauseGUI();
			Time.timeScale = 0.0f;
            gameState = GameState.PAUSE;
            Debug.Log("Game Paused");
        }
        else
        {
            Time.timeScale = 1f;
            gameState = GameState.PLAYING;
            Debug.Log("Game Playing");
        }
    }

    void GameMainMenu()
    {

        if (Input.anyKeyDown)
        {
            useGUILayout = true;
			Application.LoadLevel("IntroScene");
            gameState = GameState.PLAYING;
        }
    }

    public static GameManager Instance
    {
        get
        {
            if (thisInstance == null)
            {
                thisInstance = new GameObject("MySingleton").AddComponent<GameManager>();
            }

            return thisInstance;
        }
    }

    public void OnApplicationQuit()
    {
        thisInstance = null;
    }
	
	public void CreateEnemies(Transform BadSpider, Transform Besourinho, Transform Abelhuda, int enemies)
	{
		Transform insectsInstantiated = null;
//		float y = 13f;
//		float z = 17f;
		float x = 0;
		
		if(insectsInstantiated == null)
		{
			for (int count = 1; count <= enemies; count++)
			{
			   if (count <= 5)
			   {
			      insectsInstantiated = Instantiate(Abelhuda) as Transform;		// um Castingo para converter para o Transform
				  insectsInstantiated.transform.Translate(new Vector3(x, 0, 0));
					
				  x += 4f;
			   }
			   else if ((count <= 10) && (count > 4))
			   {
				  if(count == 6)
				  {
					  x = 0;
					  //y += -5f;
				  }
				  insectsInstantiated = Instantiate(BadSpider) as Transform;		// um Castingo para converter para o Transform
				  insectsInstantiated.transform.Translate(new Vector3(x, 0, 0));
					
				  x += 4f;
			   }
			   else
			   {
				  if(count == 11)
				  {
					  x = 0;
					  //y += -5f;
				  }
				  insectsInstantiated = Instantiate(Besourinho) as Transform;		// um Castingo para converter para o Transform
				  insectsInstantiated.transform.Translate(new Vector3(x, 0, 0));
					
				  x += 4f;
			   }
			   insectsInstantiated.tag = "Enemy";
			}
		}
	}
}
